package com.webermagem.DAO;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.webermagem.model.Usuario;
import com.webermagem.util.HibernateUtil;

public class UsuarioDAO extends GenericDAO implements Serializable{

	private static final long serialVersionUID = 2222451465499989512L;

	public UsuarioDAO(){
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Usuario> ListByUsername(Session session, String username) {
		
		if (session == null){
			session = HibernateUtil.getSessionFactory().openSession();
		}
		
		Query query = session.createQuery(" from Usuario where lower(username) like :username order by id");
		query.setString("username", "%" + username.toLowerCase() + "%");
		List<Usuario> objetoRetornado = query.list();
		session.flush();
		
		return objetoRetornado;
	}
	
	public Object getByUsername(Session session, String username) {
		
		if (session == null){
			session = HibernateUtil.getSessionFactory().openSession();
		}
		
		Query query = session.createQuery("from UsuarioSessao where username = :username order by id");
		query.setString("username", "%" + username.toLowerCase() + "%");
		query.setParameter("username", username);
		Object objetoRetornado = query.uniqueResult();
		
		session.flush();
		
		return objetoRetornado;
	}
	
	@SuppressWarnings("unchecked")
	public List<Usuario> listOrdenado(Session session) {
		
		if (session == null){
			session = HibernateUtil.getSessionFactory().openSession();
		}
		
		Query query = session.createQuery(" from Usuario order by id");
		List<Usuario> objetoRetornado = query.list();
		session.flush();
		
		return objetoRetornado;
		
	}

	
}
